package karaoke.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.Cliente;
import karaoke.modelo.Local;
import karaoke.modelo.Reserva;
import karaoke.negocio.GestionLocales;
import karaoke.negocio.GestionReserva;

/**
 * Servlet implementation class BusqReservasServlet
 */
@WebServlet("/BusqReservasServlet")
public class BusqReservasServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BusqReservasServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		ArrayList<String> errors = new ArrayList<String>();
		RequestDispatcher rd = null;
		
		String idlocal = request.getParameter("sellocal");
		String busqueda = request.getParameter("txtsearch");
		try {
			
			HttpSession sesion = request.getSession();
			Cliente c = (Cliente)sesion.getAttribute("CLIENTE");
			
			GestionReserva negocioR = new GestionReserva();
			Collection<Reserva> reservasCli = negocioR.listByCodNomSalaReservaByIdCliente(c.getIdcliente(), idlocal, busqueda);
			
			request.setAttribute("RESERVAS", reservasCli);
			
			GestionLocales negocioL = new GestionLocales();
			Collection<Local> locales = negocioL.listar();
			request.setAttribute("LOCALES", locales);
			request.setAttribute("selectedLocal", idlocal);
			
			rd = request.getRequestDispatcher("/reservas.jsp");
			
		} catch (DAOExcepcion e) {
			errors.add("Fallo el ingreso");
		}
		
		request.setAttribute("ERRORS", errors);
		rd.forward(request, response);
	}

}
